package com.letwal.cmcvnd.components.dialog

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.databinding.DialogEnterTransferPasswordBinding

class EnterTransferPasswordDialog : DialogFragment() {

    //布局
    lateinit var mViewBinding: DialogEnterTransferPasswordBinding

    //标题
    private var mTitle: String = ""

    //点击确认监听
    private var mOnConfirmListener: (String) -> Unit = { }

    companion object {

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        mViewBinding =
            DialogEnterTransferPasswordBinding.inflate(layoutInflater)
        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initSubViews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        if (mTitle.isBlank()) {
            mTitle = requireContext().getString(R.string.transfer_enter_password)
        }
        mViewBinding.textTransferPasswordTitle.text = mTitle
    }

    private fun initSubViews() {
        mViewBinding.textTransferPasswordConfirm.setOnClickListener {
            val inputValue = mViewBinding.editTransferPassword.text.toString().trim()
            if (inputValue.isNotBlank()) {
                mViewBinding.textTransferPasswordErr.visibility = View.GONE
                mOnConfirmListener.invoke(inputValue)
            } else {
                mViewBinding.textTransferPasswordErr.visibility = View.VISIBLE
            }
        }

        //显示与隐藏密码
        mViewBinding.imageTransferPasswordEye.setOnClickListener {
            val iTag = mViewBinding.imageTransferPasswordEye.tag as String
            val eText = mViewBinding.editTransferPassword.text.toString().trim()
            if (iTag.equals("close", true)) {
                mViewBinding.editTransferPassword.transformationMethod =
                    (HideReturnsTransformationMethod.getInstance())

                mViewBinding.imageTransferPasswordEye.tag = "open"
                mViewBinding.editTransferPassword.setSelection(eText.length)
                mViewBinding.imageTransferPasswordEye.setImageResource(R.drawable.icon_input_eye)

            } else {
                mViewBinding.editTransferPassword.transformationMethod =
                    (PasswordTransformationMethod.getInstance())

                mViewBinding.imageTransferPasswordEye.tag = "close"
                mViewBinding.editTransferPassword.setSelection(eText.length)
                mViewBinding.imageTransferPasswordEye.setImageResource(R.drawable.icon_input_no_eye)
            }
        }
    }

    /** 设置点击事件 */
    fun setConfirmClickListener(listener: (String) -> Unit) {
        mOnConfirmListener = listener
    }

    /** 显示弹框 */
    fun showDialog(fragmentManager: FragmentManager, title: String = "") {
        mTitle = title
        showNow(fragmentManager, EnterTransferPasswordDialog::class.java.simpleName)
    }
}